package com.bcntouch.touch2act.User;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.provider.BaseColumns;

import com.bcntouch.touch2act.Constants.Constants;
import com.bcntouch.touch2act.Data.DBHelper;

public class UserStorage {

	public static final String 	URI_PATH 			= "user";
	public static final Uri    	CONTENT_URI 		= Uri.parse(Constants.CONTENT_SCHEME + Constants.AUTHORITY + "/" + URI_PATH);
	
	public static final class UserMetaData implements BaseColumns {
			public static final String TABLE_NAME 					= URI_PATH;
			
			public static final String NAME							= "name";
			public static final String TEXT							= "text";
	}
	
	// These are the User columns that we will retrieve.
    static final String[] USER_SUMMARY_PROJECTION = new String[] {
    	UserMetaData._ID,
    	UserMetaData.NAME,
    	UserMetaData.TEXT
    };
	
	public static void initStorage(SQLiteDatabase db) {
		// Create the Products Table
		try {
			db.execSQL( "CREATE TABLE " + UserMetaData.TABLE_NAME + " ("
						+ UserMetaData._ID 					+ " INTEGER PRIMARY KEY,"
						+ UserMetaData.NAME 				+ " TEXT,"
						+ UserMetaData.TEXT       			+ " TEXT"
						+ ");"
					);				
		}
		catch ( Exception e ) {
//			Log.e("ProductsStorage " + " Error creating Database for products" );
		}
	}
	
	public static void insertUser(Context context, User user) {
		// get the database and run the query
		DBHelper sDB = DBHelper.getHelper(context);
		SQLiteDatabase db = sDB.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put(UserMetaData.NAME, user.getName());
		values.put(UserMetaData.TEXT, "testing");
		
		db.insert( UserMetaData.TABLE_NAME, UserMetaData._ID /* nullColumnHack */, values);
	}
	
	public static User getCurrentUser(Context context) {
		// get the database and run the query
		DBHelper sDB = DBHelper.getHelper(context);
		SQLiteDatabase db = sDB.getReadableDatabase();
		
		Cursor cur = db.query( UserMetaData.TABLE_NAME, USER_SUMMARY_PROJECTION, null, null, null, null, null);
		
		User currentUser = null;
		
		if ( cur.moveToFirst() ) {
			int col;
			col = cur.getColumnIndex( UserMetaData.NAME );
			String name = cur.getString( col );
			cur.close();
			currentUser = new User(name);
		}
		
		return currentUser;
	}
	
	
	
}
